package com.hd.pzhwlxt.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hd.pzhwlxt.pojo.StaffBasicInfo;
import com.hd.pzhwlxt.service.StaffBasicInfoService;
import com.hd.pzhwlxt.utils.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @create: 2022-09-20 10:44
 * @author: 怀念天空
 * @Description: 人员信息表，department部门，power权限，1管理员，0员工
 */
@RestController
@RequestMapping("/staff")
public class StaffInfoController {

    @Autowired
    private StaffBasicInfoService staffBasicInfoService;

    @GetMapping("/get")
    public Response<Object> getAllStaff(@RequestParam(value = "pageNum",required = false) Integer pageNum, @RequestParam(value = "pageSize",required = false) Integer pageSize){
        if (pageNum == null || pageSize == null){
            List<StaffBasicInfo> staffs = staffBasicInfoService.getAllStaffInfo();
            if(staffs!=null){
                return Response.createSuccessResponse("查询成功", staffs);
            }
        }else {
            Page<StaffBasicInfo> staffs = staffBasicInfoService.getAllStaffInfoPage(pageNum,pageSize);
            if(staffs!=null){
                return Response.createSuccessResponse("查询成功", staffs);
            }
        }
        return Response.createErrorResponse("查询失败");
    }

    @GetMapping("/get/{id}")
    public Response<StaffBasicInfo> getStaffInfoById(@PathVariable Integer id){
        StaffBasicInfo staff = staffBasicInfoService.getById(id);
        if(staff!=null){
            return Response.createSuccessResponse("查询成功", staff);
        }
        return Response.createErrorResponse("查询失败");
    }

    @PostMapping("/add")
    public Response<String> addStaffInfo(@RequestBody StaffBasicInfo staffBasicInfo){
        boolean save = staffBasicInfoService.save(staffBasicInfo);
        if (save){
            return Response.createSuccessResponse("添加成功");
        }
        return Response.createErrorResponse("添加失败");
    }

    @DeleteMapping("/delmore/{ids}")
    public Response<String> deleteStaffInfoByIds(@PathVariable Integer[] ids){
        boolean remove = staffBasicInfoService.removeByIds(Arrays.asList(ids));
        if (remove){
            return Response.createSuccessResponse("删除成功");
        }
        return Response.createErrorResponse("删除失败");
    }

    @PutMapping("/update")
    public Response<String> updateStaffInfo(@RequestBody StaffBasicInfo staffBasicInfo){
        boolean update = staffBasicInfoService.updateById(staffBasicInfo);
        if (update){
            return Response.createSuccessResponse("修改成功");
        }
        return Response.createErrorResponse("修改失败");
    }

    /**
     * 查询维修部管理员；
     */
    @GetMapping("/repair/admin")
    public Response<Object> getRepairAdmin(){
        List<StaffBasicInfo> list = staffBasicInfoService.getStaffByDepartAndPower("维修部",1);
        if (list != null){
         return Response.createSuccessResponse("查询成功",list);
        }
        return Response.createErrorResponse("查询失败");
    }

    @GetMapping("/repair/emp")
    public Response<Object> getRepairEmp(){
        List<StaffBasicInfo> list = staffBasicInfoService.getStaffByDepartAndPower("维修部",0);
        if (list != null){
            return Response.createSuccessResponse("查询成功",list);
        }
        return Response.createErrorResponse("查询失败");
    }


    @GetMapping("/purchase/admin")
    public Response<Object> getPurchaseAdmin(){
        List<StaffBasicInfo> list = staffBasicInfoService.getStaffByDepartAndPower("采购部",1);
        if (list != null){
            return Response.createSuccessResponse("查询成功",list);
        }
        return Response.createErrorResponse("查询失败");
    }

    @GetMapping("/purchase/emp")
    public Response<Object> getPurchaseEmp(){
        List<StaffBasicInfo> list = staffBasicInfoService.getStaffByDepartAndPower("采购部",0);
        if (list != null){
            return Response.createSuccessResponse("查询成功",list);
        }
        return Response.createErrorResponse("查询失败");
    }
}
